package com.module.prms.manager;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.ResolveUnit;

import com.module.prms.ReqBaseBean;
import com.module.prms.dao.RoleEntEventDao;
import com.module.prms.vo.PrmsRoleEntEvent;
import com.module.prms.vo.PrmsRoleFlowList;




/**
*
* <溯源流程、溯源流程操作项-角色操作并联服务类>
* 
* @author  wuwl
* @version  [1.0, July 15, 2011]
* @see  [相关类/方法]
* @since  [二代产品/权限组件]
*/	
public class RoleEntEventManager extends GenericHibernateManager<PrmsRoleEntEvent, String> {
	static Logger logger = Logger.getLogger(RoleEntEventManager.class.getName ()) ;
	RoleEntEventDao dao = RoleEntEventDao.getInstance();
	
	private RoleEntEventManager(){}
	
	private static RoleEntEventManager manager;
	
	public synchronized static RoleEntEventManager getInstance(){
		if(manager == null )
			manager = new RoleEntEventManager();
		return manager;
	} 

///**
// * <删除>
// * <将角色操作并联信息从数据库删除>
// * @param bean
// * @see [类、类#方法、类#成员]
// */
//public void del(PrmsRoleEntEvent obj)throws Exception
//{
//	this.delete(obj);
//}
//
///**
// * <删除>
// * <将角色操作并联信息从数据库删除>
// * @param delhql
// * @see [类、类#方法、类#成员]
// */
//public void delbyhql(String  delhql)throws Exception
//{
//	this.deleteByQuery(delhql);
//}
//
///**
// * <修改>
// * <修改数据库中的角色操作并联信息>
// * @param bean
// * @see [类、类#方法、类#成员]
// */
//public void upd(PrmsRoleEntEvent obj)throws Exception
//{
//	this.update(obj);
//}


/**
 * 根据角色查询设备管理权限
 * @param bean
 * @throws Exception
 */
public List<PrmsRoleEntEvent> listRoleEntEvent(String roleid)throws Exception
{
//	Map<String,Object> map = bean.getResult();
	Session session = this.getSession();
	List<PrmsRoleEntEvent> roleEntEventList = null;
	
	try {
		roleEntEventList =  dao.getRoleEntEvent(session,roleid);
	} catch (Exception e) {
		throw e;
	} finally {
		this.closeSession();
	}
	
	//多个溯源流程和溯源流选择项
	return roleEntEventList;
//	map.put("roleEntEventList",roleEntEventList);
//	
//	bean.setResult(map);
}
/**
 * 企业用户查询本企业的溯源流程权限列表
 * @param bean
 * @throws Exception
 */
public List listFlowEvent(String roleid) throws Exception
{
	Session session = this.getSession();
	List<PrmsRoleFlowList> list = null;
	try{
		list = dao.findRoleEntEventByOrgId(session,roleid);
		
	}catch(Exception e){
		throw e;
	}finally {
		this.closeSession();
	}
	return list;
}

/**
 * 保存企业的溯源流程操作权限
 * @param bean
 * @throws Exception
 */
public Map roleEntEventSave(List flowlist,String roleid) throws Exception{
	Session session = this.getSession();
	String delhql = "delete from PrmsRoleEntEvent as obj where obj.roleId='"+roleid+"'";
	boolean isTrue =false;
	Transaction tx = null;
	tx = session.beginTransaction();
	try {
		dao.deleteByQuery(session,delhql);
		PrmsRoleEntEvent roleoptobj = new PrmsRoleEntEvent();
		Date now = new Date();
		for(int j=0;j<flowlist.size();j++)
		{
			 roleoptobj=  (PrmsRoleEntEvent) ResolveUnit.resolve(ResolveUnit.getJsonStr(flowlist.get(j)), PrmsRoleEntEvent.class);
			 roleoptobj.setUpdatetime(now);
			 roleoptobj.setCreateTime(now);
			 roleoptobj.setStatus("C");
			if(roleoptobj.getEventId()!=0)
			{
				isTrue =dao.save(session, roleoptobj);
				
			}else{
				isTrue = true;
			}
		}
		tx.commit();
	} catch (Exception e) {
		tx.rollback();
		throw e;
	} finally {
		this.closeSession();
	}
	Map<String, Object> statusMap = new HashMap<String, Object>();
	String status = RequestManager.getSaveStr(isTrue);
	statusMap.put("state", isTrue);
	statusMap.put("msg", status);
	return statusMap;
}

@Override
public GenericHibernateDao<PrmsRoleEntEvent, String> getDao() {
	return dao;
}

}
